﻿using System;
using System.Web;
using Newtonsoft.Json;
using eCommerce.core.Helpers;
using eCommerce.db.BORespository;
using eCommerce.db.DataRespository;
using eCommerce.log;

namespace eCommerce.db.Action
{
    public class NoteAction : IBaseAction
    {
        public void Do(HttpContext context)
        {
            var actionName = Utils.GetIdFromRequestString(Utils.Action, string.Empty);

            var responseData = new ResponseData();
            switch (actionName)
            {
                case "getlistbystatus":
                    responseData = GetListByStatus();
                    break;
                case "getnote":     
                    responseData = GetNote();
                    break;

                case "save":
                    responseData = Save();
                    break;
                case "delete":
                    responseData = Delete();
                    break;

                case "":
                    responseData.Success = false;
                    responseData.Message = "Can not found this action.";
                    break;
            }

            var jSonString = JsonConvert.SerializeObject(responseData, Utils.SerializeFormat("MM/dd/yyyy HH:mm:ss"));
            if (!string.IsNullOrEmpty(context.Request.Params["callback"]))
                jSonString = string.Format("{0}({1})", context.Request.Params["callback"], jSonString);
            context.Response.Write(jSonString);
        }

        private static ResponseData GetListByStatus()
        {
            var responseData = new ResponseData();

            var status   = Utils.GetIdFromRequestString(Utils.Status, 0);
            var userName = Utils.GetIdFromRequestString(Utils.UserName, string.Empty);

            try
            {
                var lstNote = NoteBo.GetByStatus(status, userName);

                responseData.Success = true;
                responseData.Data = lstNote;
                responseData.TotalRow = lstNote.Count;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }

            return responseData;
        }

        private static ResponseData GetNote()
        {
            var responseData = new ResponseData();
            var id = Utils.GetIdFromRequestString(Utils.NoteId, 0);

            try
            {
                var size = NoteBo.GetById(id);

                responseData.Success = true;
                responseData.Data = size;
                responseData.TotalRow = 1;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }

        private static ResponseData Save()
        {
            var responseData = new ResponseData();

            var title    = Utils.GetIdFromRequestString(Utils.Title, string.Empty);
            var content  = Utils.GetIdFromRequestString(Utils.Content, string.Empty);
            var userName = Utils.GetIdFromRequestString(Utils.UserName, string.Empty);
            var status   = Utils.GetIdFromRequestString(Utils.Status, 0);

            try
            {
                var o = new NoteEntity();
                o.Title     = title;
                o.Content   = content;
                o.CreatedBy = userName;
                o.Status    = status;

                NoteBo.Save(o);

                responseData.Success = true;
                responseData.Data = null;
                responseData.TotalRow = 0;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }

        private static ResponseData Delete()
        {
            var responseData = new ResponseData();

            var id = Utils.GetIdFromRequestString(Utils.NoteId, 0);

            try
            {
                NoteBo.Delete(id);

                responseData.Success = true;
                responseData.Data = null;
                responseData.TotalRow = 0;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }
    }
}
